<?php
	defined('RUN') or die('No access');

	class SupportController
	{
		public function __construct()
		{
			if (!Auth::check('acp'))
			{
				Url::redirect('admin/auth/login');
			}
		}
		
		public function index($page = 1)
		{
			$data = Model::get('admin')->getLayoutData();
			$fields='`id`,`name`,`yahoo`,`skype`,`email`';
			$data['supports'] = Model::get('support')->getSupports($fields);
			if (Session::getFlash('notification'))
			{
				$data['notification']=Session::getFlash('notification');
			}
			$data['content_file'] = 'support/view.tpl';
			View::load('layout.tpl', $data, 'admin');
		}

		public function add()
		{
			$data = Model::get('admin')->getLayoutData();

			if (Input::post('action') == 'add' && Security::checkToken('token_form', Input::post('token')))
			{
				if (Validator::checkRequired(Input::post(), array('txtName','txtYahoo','txtEmail')))
				{
					$data = array(
						'name' 			=> strip_tags(Input::post('txtName')),
						'yahoo' 		=> strip_tags(Input::post('txtYahoo')),
						'phone' 		=> strip_tags(Input::post('txtPhone')),
						'email' 		=> strip_tags(Input::post('txtEmail')),
						'skype' 		=> strtolower(Input::post('txtSkype')),
						'sort' 			=> (int)Input::post('txtSort'),							
					);
						
					if (Model::get('support')->add($data))
					{
						Session::setFlash('notification', array('type' => 'green', 'msg' => 'Đã thêm thành công !'));
						Url::redirect('admin/support');
					}
				}

				$data['failed'] = TRUE;
			}
                        
			$data['action'] = 'add';
			$data['content_file'] = 'support/form.tpl';
			View::load('layout.tpl', $data, 'admin');
		}
        
        public function update($id)
		{
			$data = Model::get('admin')->getLayoutData();
            $data['supports'] = Model::get('support')->getById($id);

			if (Input::post('action') == 'update' && Security::checkToken('token_form', Input::post('token')))
			{
				if (Validator::checkRequired(Input::post(), array('txtName','txtYahoo','txtEmail')))
				{
					$data = array(
						'name' 			=> strip_tags(Input::post('txtName')),
						'yahoo' 		=> strip_tags(Input::post('txtYahoo')),
						'phone' 		=> strip_tags(Input::post('txtPhone')),
						'email' 		=> strip_tags(Input::post('txtEmail')),
						'skype' 		=> strtolower(Input::post('txtSkype')),
						'sort' 			=> (int)Input::post('txtSort'),							
					);

					if (Model::get('support')->update($data, $id))
					{
						Session::setFlash('notification', array('type' => 'green', 'msg' => 'Chỉnh sửa thành công !'));
						Url::redirect('admin/support');
					}else{
						Session::setFlash('notification', array('type' => 'red', 'msg' => 'Chỉnh sửa không thành công !'));
						Url::redirect('admin/support');
					}
				}

				$data['failed'] = TRUE;
			}
            
			$data['action'] = 'update';
			$data['content_file'] = 'support/form.tpl';
			View::load('layout.tpl', $data, 'admin');
		}                

		public function delete($id, $confirm = 'no')
		{
			$data = Model::get('admin')->getLayoutData();

			if ($confirm == 'yes')
			{     
                if (Model::get('support')->delete($id))
				{
					Session::setFlash('notification', array('type' => 'green', 'msg' => 'Xoá thành công!'));
					Url::redirect('admin/support');
				}
			}

			$data['supports'] = Model::get('support')->getById($id);
			$data['content_file'] = 'support/delete.tpl';
			View::load('layout.tpl', $data, 'admin');
		}
	}

